package com.example.pixelyear.ui.act

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.example.pixelyear.R
import com.example.pixelyear.sql.ColorSqlUtils
import com.example.pixelyear.ui.base.BaseActivity
import com.example.pixelyear.utils.*
import kotlinx.android.synthetic.main.activity_splash.*

class SplashActivity : BaseActivity() {
    private val time = 2000L

    override fun getLayoutResID(): Int {
        return R.layout.activity_splash
    }

    override fun initView(bundle: Bundle?) {
        Utils.hideBottomUIMenu(mActivity)
        FunctionUtils.checkServiceTime(mActivity)
    }

    override fun initData() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
            YxsSpUtils.putBoolean(
                Constant.SD, ContextCompat.checkSelfPermission(
                    mActivity,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED
            )
        else YxsSpUtils.putBoolean(Constant.SD, true)

        if (YxsSpUtils.getBoolean(Constant.FIRST_ADD_DEFAULT_COLOR, true)) {
            ColorSqlUtils.getInstance(mActivity)?.addDefault()
            YxsSpUtils.putBoolean(Constant.FIRST_ADD_DEFAULT_COLOR, false)
        }

        if (YxsSpUtils.getBoolean(Constant.SD) && YxsSpUtils.getString(Constant.SPLASH_IMG).isNotEmpty()) {
            Glide.with(mActivity).load(YxsSpUtils.getString(Constant.SPLASH_IMG)).into(Splash)
        }
    }

    override fun initListener() {
        DefaultHandler.getInstance().postDelayed({
            YxsDisplay.toMain(mActivity, true)
        }, time)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            true
        } else
            super.onKeyDown(keyCode, event)
    }
}
